package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_wjjhl.domain.SnhshjlBaDetail;
import com.ruoyi.report_wjjhl.domain.vo.SnhshjlBaDetailVo;
import com.ruoyi.report_wjjhl.mapper.SnhshjlBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.SnhshjlbgBsInfoBo;
import com.ruoyi.report_wjjhl.domain.vo.SnhshjlbgBsInfoVo;
import com.ruoyi.report_wjjhl.domain.SnhshjlbgBsInfo;
import com.ruoyi.report_wjjhl.mapper.SnhshjlbgBsInfoMapper;
import com.ruoyi.report_wjjhl.service.ISnhshjlbgBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 水泥或石灰剂量试验检测报告EDTA滴定法Service业务层处理
 *
 * @author zyb
 * @date 2024-08-02
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class SnhshjlbgBsInfoServiceImpl implements ISnhshjlbgBsInfoService {

    private final SnhshjlbgBsInfoMapper baseMapper;
    private final SnhshjlBaDetailMapper detailMapper;

    /**
     * 查询水泥或石灰剂量试验检测报告EDTA滴定法
     */
    @Override
    @FIllBasicInfo
    public SnhshjlbgBsInfoVo queryById(Long excelId){
        SnhshjlbgBsInfoVo snhshjlbgBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<SnhshjlBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<SnhshjlBaDetail>()
            .eq(SnhshjlBaDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            snhshjlbgBsInfoVo.setBaDetailVos(baDetailVos);
        }
        // 如果已经有值了则不需要计算结论。
        if (StrUtil.isNotEmpty(snhshjlbgBsInfoVo.getJiancjl()) && !"检测结论：".equals(snhshjlbgBsInfoVo.getJiancjl())) {
            return snhshjlbgBsInfoVo;
        }
        return jcjl(snhshjlbgBsInfoVo);
    }

    /**
     * 查询水泥或石灰剂量试验检测报告EDTA滴定法列表
     */
    @Override
    public TableDataInfo<SnhshjlbgBsInfoVo> queryPageList(SnhshjlbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SnhshjlbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<SnhshjlbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水泥或石灰剂量试验检测报告EDTA滴定法列表
     */
    @Override
    public List<SnhshjlbgBsInfoVo> queryList(SnhshjlbgBsInfoBo bo) {
        LambdaQueryWrapper<SnhshjlbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SnhshjlbgBsInfo> buildQueryWrapper(SnhshjlbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SnhshjlbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, SnhshjlbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, SnhshjlbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), SnhshjlbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), SnhshjlbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), SnhshjlbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), SnhshjlbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), SnhshjlbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), SnhshjlbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), SnhshjlbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), SnhshjlbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancrq()), SnhshjlbgBsInfo::getJiancrq, bo.getJiancrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), SnhshjlbgBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), SnhshjlbgBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), SnhshjlbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), SnhshjlbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), SnhshjlbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getQuywz()), SnhshjlbgBsInfo::getQuywz, bo.getQuywz());
        lqw.eq(StringUtils.isNotBlank(bo.getWeitrwbh()), SnhshjlbgBsInfo::getWeitrwbh, bo.getWeitrwbh());
        lqw.eq(StringUtils.isNotBlank(bo.getTuycd()), SnhshjlbgBsInfo::getTuycd, bo.getTuycd());
        lqw.eq(StringUtils.isNotBlank(bo.getDaibsl()), SnhshjlbgBsInfo::getDaibsl, bo.getDaibsl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), SnhshjlbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), SnhshjlbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), SnhshjlbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), SnhshjlbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), SnhshjlbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), SnhshjlbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), SnhshjlbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), SnhshjlbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getHuigfcs()), SnhshjlbgBsInfo::getHuigfcs, bo.getHuigfcs());
        lqw.eq(bo.getCansa() != null, SnhshjlbgBsInfo::getCansa, bo.getCansa());
        lqw.eq(bo.getCansb() != null, SnhshjlbgBsInfo::getCansb, bo.getCansb());
        lqw.eq(bo.getCansc() != null, SnhshjlbgBsInfo::getCansc, bo.getCansc());
        lqw.eq(bo.getShiycs() != null, SnhshjlbgBsInfo::getShiycs, bo.getShiycs());
        lqw.eq(bo.getZuidz() != null, SnhshjlbgBsInfo::getZuidz, bo.getZuidz());
        lqw.eq(bo.getZuixz() != null, SnhshjlbgBsInfo::getZuixz, bo.getZuixz());
        lqw.eq(bo.getPingjz() != null, SnhshjlbgBsInfo::getPingjz, bo.getPingjz());
        lqw.eq(bo.getBiaozc() != null, SnhshjlbgBsInfo::getBiaozc, bo.getBiaozc());
        lqw.eq(bo.getPiancxs() != null, SnhshjlbgBsInfo::getPiancxs, bo.getPiancxs());
        lqw.eq(bo.getQualId() != null, SnhshjlbgBsInfo::getQualId, bo.getQualId());
        lqw.eq(StringUtils.isNotBlank(bo.getSymbolValue()), SnhshjlbgBsInfo::getSymbolValue, bo.getSymbolValue());
        lqw.eq(bo.getNum1() != null, SnhshjlbgBsInfo::getNum1, bo.getNum1());
        lqw.eq(bo.getNum2() != null, SnhshjlbgBsInfo::getNum2, bo.getNum2());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszb()), SnhshjlbgBsInfo::getJiszb, bo.getJiszb());
        lqw.eq(bo.getJiancjg() != null, SnhshjlbgBsInfo::getJiancjg, bo.getJiancjg());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpd()), SnhshjlbgBsInfo::getJiegpd, bo.getJiegpd());
        return lqw;
    }

    /**
     * 新增水泥或石灰剂量试验检测报告EDTA滴定法
     */
    @Override
    public Boolean insertByBo(SnhshjlbgBsInfoBo bo) {
        SnhshjlbgBsInfo add = BeanUtil.toBean(bo, SnhshjlbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 10次
            ArrayList<SnhshjlBaDetail> details = new ArrayList<>(10);
            for (long i = 1; i <= 10L; i++) {
                details.add(new SnhshjlBaDetail(add.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改水泥或石灰剂量试验检测报告EDTA滴定法
     */
    @Override
    @ComputeReportResult
    public Boolean updateByBo(SnhshjlbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        SnhshjlbgBsInfo update = BeanUtil.toBean(bo, SnhshjlbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), SnhshjlBaDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(SnhshjlbgBsInfoBo bo) {
        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
            for (SnhshjlBaDetailVo detailVo : bo.getBaDetailVos()) {
                if ("不符合".equals(detailVo.getJiegpd())) {
                    bo.setExcelResult(2);
                    return;
                }
            }
            bo.setExcelResult(1);
        }
        bo.setExcelResult(0);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SnhshjlbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水泥或石灰剂量试验检测报告EDTA滴定法
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 生成 水泥或石灰剂量试验检测报告 检测结论
     */
    @Override
    public SnhshjlbgBsInfoVo jcjl(SnhshjlbgBsInfoVo vo) {
        //        if (StrUtil.isEmpty(vo.getPandyj())) {
//            return vo;
//        }
        vo.setJiancjl("检测结论：经检测，所检指标符合《水泥稳定碎石基层施工技术规范》DB37/T 3577-2019及设计文件要求。");
        return vo;
    }
}
